﻿using System;
using System.Collections.Generic;
using System.Text;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Communication.Base;

namespace UtilZ.Dotnet.Ex.Communication.Net.Base
{
    /// <summary>
    /// UDP分包器-收到一个包就直接输出
    /// </summary>
    public class BMQUDPUnpacker : UnpackerAbs
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public BMQUDPUnpacker()
        {

        }

        /// <summary>
        /// 对数据进行拆包
        /// </summary>
        /// <param name="unpackerPara">分包参数</param>
        /// <returns>拆包成功的数据长度</returns>
        protected override int PrimitiveUnPackge(IUnpackerPara unpackerPara)
        {
            var np = (UdpUnpackerPara)unpackerPara;
            if (np.DataLength <= 0)
            {
                return np.DataLength;
            }

            byte[] dataBuffer =new byte[np.DataLength];
            Array.Copy(np.Buffer, np.Offset, dataBuffer, 0, dataBuffer.Length);
            var args = new DataOutputArgs(unpackerPara.LinkInfo, new Model.BufferData(dataBuffer, 0, dataBuffer.Length));
            base.OnRaiseOnReceivedData(np.Client, args);
            return np.DataLength;
        }
    }
}
